package com.ldg.crm.dao;

import com.ldg.crm.entity.ContactsEntity;
import com.ldg.crm.entity.ContactsEntityExample.Criteria;
import com.ldg.crm.entity.ContactsEntityExample.Criterion;
import com.ldg.crm.entity.ContactsEntityExample;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;

public class ContactsEntitySqlProvider {
    /**
     *
     * @mbg.generated
     */
    public String countByExample(ContactsEntityExample example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("crm_contacts");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String deleteByExample(ContactsEntityExample example) {
        SQL sql = new SQL();
        sql.DELETE_FROM("crm_contacts");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String insertSelective(ContactsEntity record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("crm_contacts");
        
        sql.VALUES("contacts_id", "#{contactsId,jdbcType=INTEGER}");
        
        if (record.getCid() != null) {
            sql.VALUES("cid", "#{cid,jdbcType=INTEGER}");
        }
        
        if (record.getBusId() != null) {
            sql.VALUES("bus_id", "#{busId,jdbcType=INTEGER}");
        }
        
        if (record.getName() != null) {
            sql.VALUES("name", "#{name,jdbcType=VARCHAR}");
        }
        
        if (record.getBirthday() != null) {
            sql.VALUES("birthday", "#{birthday,jdbcType=DATE}");
        }
        
        if (record.getPost() != null) {
            sql.VALUES("post", "#{post,jdbcType=VARCHAR}");
        }
        
        if (record.getDepartment() != null) {
            sql.VALUES("department", "#{department,jdbcType=VARCHAR}");
        }
        
        if (record.getSex() != null) {
            sql.VALUES("sex", "#{sex,jdbcType=VARCHAR}");
        }
        
        if (record.getSaltName() != null) {
            sql.VALUES("salt_name", "#{saltName,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilePhone1() != null) {
            sql.VALUES("mobile_phone1", "#{mobilePhone1,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilePhone2() != null) {
            sql.VALUES("mobile_phone2", "#{mobilePhone2,jdbcType=VARCHAR}");
        }
        
        if (record.getWorkPhone() != null) {
            sql.VALUES("work_phone", "#{workPhone,jdbcType=VARCHAR}");
        }
        
        if (record.getWx() != null) {
            sql.VALUES("wx", "#{wx,jdbcType=VARCHAR}");
        }
        
        if (record.getQq() != null) {
            sql.VALUES("qq", "#{qq,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            sql.VALUES("email", "#{email,jdbcType=VARCHAR}");
        }
        
        if (record.getAddress() != null) {
            sql.VALUES("address", "#{address,jdbcType=VARCHAR}");
        }
        
        if (record.getZipCode() != null) {
            sql.VALUES("zip_code", "#{zipCode,jdbcType=VARCHAR}");
        }
        
        if (record.getCreateTime() != null) {
            sql.VALUES("create_time", "#{createTime,jdbcType=DATE}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.VALUES("update_time", "#{updateTime,jdbcType=DATE}");
        }
        
        if (record.getDescription() != null) {
            sql.VALUES("description", "#{description,jdbcType=LONGVARCHAR}");
        }
        
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String selectByExampleWithBLOBs(ContactsEntityExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("contacts_id");
        } else {
            sql.SELECT("contacts_id");
        }
        sql.SELECT("cid");
        sql.SELECT("bus_id");
        sql.SELECT("name");
        sql.SELECT("birthday");
        sql.SELECT("post");
        sql.SELECT("department");
        sql.SELECT("sex");
        sql.SELECT("salt_name");
        sql.SELECT("mobile_phone1");
        sql.SELECT("mobile_phone2");
        sql.SELECT("work_phone");
        sql.SELECT("wx");
        sql.SELECT("qq");
        sql.SELECT("email");
        sql.SELECT("address");
        sql.SELECT("zip_code");
        sql.SELECT("create_time");
        sql.SELECT("update_time");
        sql.SELECT("description");
        sql.FROM("crm_contacts");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String selectByExample(ContactsEntityExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("contacts_id");
        } else {
            sql.SELECT("contacts_id");
        }
        sql.SELECT("cid");
        sql.SELECT("bus_id");
        sql.SELECT("name");
        sql.SELECT("birthday");
        sql.SELECT("post");
        sql.SELECT("department");
        sql.SELECT("sex");
        sql.SELECT("salt_name");
        sql.SELECT("mobile_phone1");
        sql.SELECT("mobile_phone2");
        sql.SELECT("work_phone");
        sql.SELECT("wx");
        sql.SELECT("qq");
        sql.SELECT("email");
        sql.SELECT("address");
        sql.SELECT("zip_code");
        sql.SELECT("create_time");
        sql.SELECT("update_time");
        sql.FROM("crm_contacts");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String updateByExampleSelective(Map<String, Object> parameter) {
        ContactsEntity record = (ContactsEntity) parameter.get("record");
        ContactsEntityExample example = (ContactsEntityExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("crm_contacts");
        
        if (record.getContactsId() != null) {
            sql.SET("contacts_id = #{record.contactsId,jdbcType=INTEGER}");
        }
        
        if (record.getCid() != null) {
            sql.SET("cid = #{record.cid,jdbcType=INTEGER}");
        }
        
        if (record.getBusId() != null) {
            sql.SET("bus_id = #{record.busId,jdbcType=INTEGER}");
        }
        
        if (record.getName() != null) {
            sql.SET("name = #{record.name,jdbcType=VARCHAR}");
        }
        
        if (record.getBirthday() != null) {
            sql.SET("birthday = #{record.birthday,jdbcType=DATE}");
        }
        
        if (record.getPost() != null) {
            sql.SET("post = #{record.post,jdbcType=VARCHAR}");
        }
        
        if (record.getDepartment() != null) {
            sql.SET("department = #{record.department,jdbcType=VARCHAR}");
        }
        
        if (record.getSex() != null) {
            sql.SET("sex = #{record.sex,jdbcType=VARCHAR}");
        }
        
        if (record.getSaltName() != null) {
            sql.SET("salt_name = #{record.saltName,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilePhone1() != null) {
            sql.SET("mobile_phone1 = #{record.mobilePhone1,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilePhone2() != null) {
            sql.SET("mobile_phone2 = #{record.mobilePhone2,jdbcType=VARCHAR}");
        }
        
        if (record.getWorkPhone() != null) {
            sql.SET("work_phone = #{record.workPhone,jdbcType=VARCHAR}");
        }
        
        if (record.getWx() != null) {
            sql.SET("wx = #{record.wx,jdbcType=VARCHAR}");
        }
        
        if (record.getQq() != null) {
            sql.SET("qq = #{record.qq,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            sql.SET("email = #{record.email,jdbcType=VARCHAR}");
        }
        
        if (record.getAddress() != null) {
            sql.SET("address = #{record.address,jdbcType=VARCHAR}");
        }
        
        if (record.getZipCode() != null) {
            sql.SET("zip_code = #{record.zipCode,jdbcType=VARCHAR}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{record.createTime,jdbcType=DATE}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.SET("update_time = #{record.updateTime,jdbcType=DATE}");
        }
        
        if (record.getDescription() != null) {
            sql.SET("description = #{record.description,jdbcType=LONGVARCHAR}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String updateByExampleWithBLOBs(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("crm_contacts");
        
        sql.SET("contacts_id = #{record.contactsId,jdbcType=INTEGER}");
        sql.SET("cid = #{record.cid,jdbcType=INTEGER}");
        sql.SET("bus_id = #{record.busId,jdbcType=INTEGER}");
        sql.SET("name = #{record.name,jdbcType=VARCHAR}");
        sql.SET("birthday = #{record.birthday,jdbcType=DATE}");
        sql.SET("post = #{record.post,jdbcType=VARCHAR}");
        sql.SET("department = #{record.department,jdbcType=VARCHAR}");
        sql.SET("sex = #{record.sex,jdbcType=VARCHAR}");
        sql.SET("salt_name = #{record.saltName,jdbcType=VARCHAR}");
        sql.SET("mobile_phone1 = #{record.mobilePhone1,jdbcType=VARCHAR}");
        sql.SET("mobile_phone2 = #{record.mobilePhone2,jdbcType=VARCHAR}");
        sql.SET("work_phone = #{record.workPhone,jdbcType=VARCHAR}");
        sql.SET("wx = #{record.wx,jdbcType=VARCHAR}");
        sql.SET("qq = #{record.qq,jdbcType=VARCHAR}");
        sql.SET("email = #{record.email,jdbcType=VARCHAR}");
        sql.SET("address = #{record.address,jdbcType=VARCHAR}");
        sql.SET("zip_code = #{record.zipCode,jdbcType=VARCHAR}");
        sql.SET("create_time = #{record.createTime,jdbcType=DATE}");
        sql.SET("update_time = #{record.updateTime,jdbcType=DATE}");
        sql.SET("description = #{record.description,jdbcType=LONGVARCHAR}");
        
        ContactsEntityExample example = (ContactsEntityExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("crm_contacts");
        
        sql.SET("contacts_id = #{record.contactsId,jdbcType=INTEGER}");
        sql.SET("cid = #{record.cid,jdbcType=INTEGER}");
        sql.SET("bus_id = #{record.busId,jdbcType=INTEGER}");
        sql.SET("name = #{record.name,jdbcType=VARCHAR}");
        sql.SET("birthday = #{record.birthday,jdbcType=DATE}");
        sql.SET("post = #{record.post,jdbcType=VARCHAR}");
        sql.SET("department = #{record.department,jdbcType=VARCHAR}");
        sql.SET("sex = #{record.sex,jdbcType=VARCHAR}");
        sql.SET("salt_name = #{record.saltName,jdbcType=VARCHAR}");
        sql.SET("mobile_phone1 = #{record.mobilePhone1,jdbcType=VARCHAR}");
        sql.SET("mobile_phone2 = #{record.mobilePhone2,jdbcType=VARCHAR}");
        sql.SET("work_phone = #{record.workPhone,jdbcType=VARCHAR}");
        sql.SET("wx = #{record.wx,jdbcType=VARCHAR}");
        sql.SET("qq = #{record.qq,jdbcType=VARCHAR}");
        sql.SET("email = #{record.email,jdbcType=VARCHAR}");
        sql.SET("address = #{record.address,jdbcType=VARCHAR}");
        sql.SET("zip_code = #{record.zipCode,jdbcType=VARCHAR}");
        sql.SET("create_time = #{record.createTime,jdbcType=DATE}");
        sql.SET("update_time = #{record.updateTime,jdbcType=DATE}");
        
        ContactsEntityExample example = (ContactsEntityExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    public String updateByPrimaryKeySelective(ContactsEntity record) {
        SQL sql = new SQL();
        sql.UPDATE("crm_contacts");
        
        if (record.getCid() != null) {
            sql.SET("cid = #{cid,jdbcType=INTEGER}");
        }
        
        if (record.getBusId() != null) {
            sql.SET("bus_id = #{busId,jdbcType=INTEGER}");
        }
        
        if (record.getName() != null) {
            sql.SET("name = #{name,jdbcType=VARCHAR}");
        }
        
        if (record.getBirthday() != null) {
            sql.SET("birthday = #{birthday,jdbcType=DATE}");
        }
        
        if (record.getPost() != null) {
            sql.SET("post = #{post,jdbcType=VARCHAR}");
        }
        
        if (record.getDepartment() != null) {
            sql.SET("department = #{department,jdbcType=VARCHAR}");
        }
        
        if (record.getSex() != null) {
            sql.SET("sex = #{sex,jdbcType=VARCHAR}");
        }
        
        if (record.getSaltName() != null) {
            sql.SET("salt_name = #{saltName,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilePhone1() != null) {
            sql.SET("mobile_phone1 = #{mobilePhone1,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilePhone2() != null) {
            sql.SET("mobile_phone2 = #{mobilePhone2,jdbcType=VARCHAR}");
        }
        
        if (record.getWorkPhone() != null) {
            sql.SET("work_phone = #{workPhone,jdbcType=VARCHAR}");
        }
        
        if (record.getWx() != null) {
            sql.SET("wx = #{wx,jdbcType=VARCHAR}");
        }
        
        if (record.getQq() != null) {
            sql.SET("qq = #{qq,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            sql.SET("email = #{email,jdbcType=VARCHAR}");
        }
        
        if (record.getAddress() != null) {
            sql.SET("address = #{address,jdbcType=VARCHAR}");
        }
        
        if (record.getZipCode() != null) {
            sql.SET("zip_code = #{zipCode,jdbcType=VARCHAR}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{createTime,jdbcType=DATE}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.SET("update_time = #{updateTime,jdbcType=DATE}");
        }
        
        if (record.getDescription() != null) {
            sql.SET("description = #{description,jdbcType=LONGVARCHAR}");
        }
        
        sql.WHERE("contacts_id = #{contactsId,jdbcType=INTEGER}");
        
        return sql.toString();
    }

    /**
     *
     * @mbg.generated
     */
    protected void applyWhere(SQL sql, ContactsEntityExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}